package org.zjx.repository.impl;


import org.zjx.db.ItemSlotDB;
import org.zjx.enums.prop.SlotType;
import org.zjx.model.prop.ItemSlot;
import org.zjx.repository.ItemSlotRepository;

import java.util.List;
import java.util.Optional;

public class ItemSlotRepositoryImpl implements ItemSlotRepository {
    private final ItemSlotDB itemSlotDB;

    public ItemSlotRepositoryImpl(ItemSlotDB itemSlotDB) {
        this.itemSlotDB = itemSlotDB;
    }

    @Override
    public List<ItemSlot> findByType(SlotType slotType) {
        return itemSlotDB.findByType(slotType);
    }

    @Override
    public Optional<ItemSlot> findById(int slotId) {
        return Optional.ofNullable(itemSlotDB.findById(slotId));
    }

    @Override
    public void save(ItemSlot slot) {
        if (itemSlotDB.findById(slot.getId()) != null) {
            itemSlotDB.update(slot);
        } else {
            itemSlotDB.insert(slot);
        }
    }

    @Override
    public void saveAll(List<ItemSlot> slots) {
        slots.forEach(this::save);
    }
}